home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / CBASE.ZIP / CBOPEN.C < prev    next >
Text File  |  1990-06-21  |  5KB  |  201 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbopen.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <string.h>*/
  9.  
  10. /* library headers */
  11. #include <blkio.h>
  12. #include <btree.h>
  13. #include <lseq.h>
  14.  
  15. /* local headers */
  16. #include "cbase_.h"
  17.  
  18. /* cbase control structure table definition */
  19. cbase_t cbb[CBOPEN_MAX];
  20.  
  21. /* cbase record position comparison function */
  22. int cbrposcmp(p1, p2, n)
  23. const void *p1;
  24. const void *p2;
  25. size_t n;
  26. {
  27.     cbrpos_t rpos1 = NIL;
  28.     cbrpos_t rpos2 = NIL;
  29.  
  30.     memcpy(&rpos1, p1, sizeof(rpos1));
  31.     memcpy(&rpos2, p2, sizeof(rpos2));
  32.     if (rpos1 < rpos2) {
  33.         return -1;
  34.     } else if (rpos1 > rpos2) {
  35.         return 1;
  36.     }
  37.  
  38.     return 0;
  39. }
  40.  
  41. /* btree field definition list */
  42. static btfield_t btfldv[] = {
  43.     {0,
  44.      0,
  45.      NULL,
  46.      BT_FASC
  47.     },
  48.     {0,
  49.      sizeof(cbrpos_t),
  50.      cbrposcmp,
  51.      BT_FASC
  52.     }
  53. };
  54.  
  55. /*man---------------------------------------------------------------------------
  56. NAME
  57.      cbopen - open a cbase
  58.  
  59. SYNOPSIS
  60.      #include <cbase.h>
  61.  
  62.      cbase_t *cbopen(cbname, type, fldc, fldv)
  63.      const char *cbname;
  64.      char *type;
  65.      int fldc;
  66.      const cbfield_t fldv[];
  67.  
  68. DESCRIPTION
  69.      The cbopen function opens the cbase named by cbname.  A pointer
  70.      to the cbase_t control structure associated with the cbase is
  71.      returned.
  72.  
  73.      type is a character string having one of the following values:
  74.  
  75.           "r"            open for reading
  76.           "r+"           open for update (reading and writing)
  77.  
  78.      See cbcreate for explanation of the field count fldc and the
  79.      field definition list fldv.
  80.  
  81.      cbopen will fail if one or more of the following is true:
  82.  
  83.      [EINVAL]       cbname is the NULL pointer.
  84.      [EINVAL]       type is not "r" or "r+".
  85.      [EINVAL]       fldc is less than 1.
  86.      [EINVAL]       fldv is the NULL pointer.
  87.      [EINVAL]       fldv contains an invalid field definition.
  88.      [CBECORRUPT]   A file in the named cbase is corrupt.
  89.      [CBEMFILE]     Too many open cbases.  The maximum is defined as
  90.                     CBOPEN_MAX in <cbase.h>.
  91.  
  92. SEE ALSO
  93.      cbclose, cbcreate.
  94.  
  95. DIAGNOSTICS
  96.      On failure cbopen returns a NULL pointer, and errno set to
  97.      indicate the error.
  98.  
  99. NOTES
  100.      The same field count and field definition list with which the
  101.      cbase was created must be used each time the cbase is opened.
  102.      Otherwise the results are undefined.
  103.  
  104. ------------------------------------------------------------------------------*/
  105. cbase_t *cbopen(cbname, type, fldc, fldv)
  106. const char *cbname;
  107. const char *type;
  108. int fldc;
  109. const cbfield_t fldv[];
  110. {
  111.     int terrno = 0;
  112.     cbase_t *cbp = NULL;
  113.     int i = 0;
  114.  
  115.     /* validate arguments */
  116.     if (cbname == NULL || type == NULL || fldc < 1 || fldv == NULL) {
  117.         errno = EINVAL;
  118.         return NULL;
  119.     }
  120.  
  121.     /* find free slot in cbb table */
  122.     for (cbp = cbb; cbp < (cbb + CBOPEN_MAX); ++cbp) {
  123.         if (!(cbp->flags & CBOPEN)) {
  124.             break;        /* found */
  125.         }
  126.     }
  127.     if (cbp >= cbb + CBOPEN_MAX) {
  128.         errno = CBEMFILE;
  129.         return NULL;        /* no free slots */
  130.     }
  131.  
  132.     /* open record file */
  133.     if (strcmp(type, CB_READ) == 0) {
  134.         cbp->flags = CBREAD;
  135.     } else if (strcmp(type, CB_RDWR) == 0) {
  136.         cbp->flags = CBREAD | CBWRITE;
  137.     } else {
  138.         errno = EINVAL;
  139.         return NULL;
  140.     }
  141.     cbp->lsp = lsopen(cbname, type);
  142.     if (cbp->lsp == NULL) {
  143.         if (errno == LSECORRUPT) errno = CBECORRUPT;
  144.         if ((errno != ENOENT) && (errno != CBECORRUPT)) CBEPRINT;
  145.         memset(cbp, 0, sizeof(*cbb));
  146.         cbp->flags = 0;
  147.         return NULL;
  148.     }
  149.  
  150.     /* validate arguments */
  151.     if (!cb_fvalid(lsrecsize(cbp->lsp), fldc, fldv)) {
  152.         lsclose(cbp->lsp);
  153.         memset(cbp, 0, sizeof(*cbb));
  154.         cbp->flags = 0;
  155.         errno = EINVAL;
  156.         return NULL;
  157.     }
  158.  
  159.     /* copy field definitions into cbase structure */
  160.     cbp->fldc = fldc;
  161.     cbp->fldv = NULL;
  162.     cbp->btpv = NULL;
  163.     if (cb_alloc(cbp) == -1) {
  164.         terrno = errno;
  165.         lsclose(cbp->lsp);
  166.         memset(cbp, 0, sizeof(*cbb));
  167.         cbp->flags = 0;
  168.         errno = terrno;
  169.         return NULL;
  170.     }
  171.     memcpy(cbp->fldv, fldv, cbp->fldc * sizeof(*cbp->fldv));
  172.  
  173.     /* open key files */
  174.     for (i = 0; i < cbp->fldc; ++i) {
  175.         if (cbp->fldv[i].flags & CB_FKEY) {
  176.             btfldv[1].offset = btfldv[0].len = cbp->fldv[i].len;
  177.             btfldv[0].cmp = cbcmpv[cbp->fldv[i].type];
  178.             cbp->btpv[i] = btopen(cbp->fldv[i].filename, type, 2, btfldv);
  179.             if (cbp->btpv[i] == NULL) {
  180.                 if ((errno != ENOENT) && (errno != BTECORRUPT)) CBEPRINT;
  181.                 if (errno == BTECORRUPT) errno = CBECORRUPT;
  182.                 terrno = errno;
  183.                 for (i--; i >= 0; i--) {
  184.                     if (cbp->fldv[i].flags & CB_FKEY) {
  185.                         btclose(cbp->btpv[i]);
  186.                     }
  187.                 }
  188.                 lsclose(cbp->lsp);
  189.                 cb_free(cbp);
  190.                 memset(cbp, 0, sizeof(*cbb));
  191.                 cbp->flags = 0;
  192.                 errno = terrno;
  193.                 return NULL;
  194.             }
  195.         }
  196.     }
  197.  
  198.     errno = 0;
  199.     return cbp;
  200. }
  201.